﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/7 星期二 22:21:22
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Sector7.Framework.Windows;
using System.ComponentModel;
using System.Windows.Controls;

namespace Sector7.Framework.Core
{
    /// <summary>
    /// 主窗口基类
    /// </summary>                
    public class MainWindowBase : Window
    {
        #region Accessor
        /// <summary>
        /// 配置信息加载完成事件
        /// </summary>
        public event EventHandler LoadConfigurationCompleted = null;

        /// <summary>
        /// 数字证书加载完成事件
        /// </summary>
        public event EventHandler LoadCredentialsCompleted = null;

        /// <summary>
        /// 依赖属性：是否启用启动窗口功能
        /// </summary>
        public static readonly DependencyProperty IsEnableSplashWindowProperty = DependencyProperty.Register("IsEnableSplashWindow", typeof(bool), typeof(MainWindowBase));

        /// <summary>
        /// 获取或设置是否启用启动窗口功能
        /// </summary>
        [Description("获取或设置是否启用启动窗口功能")]
        [Category("其他")]
        public bool IsEnableSplashWindow
        {
            get { return Convert.ToBoolean(this.GetValue(IsEnableSplashWindowProperty)); }
            set { this.SetValue(IsEnableSplashWindowProperty, value); }
        }
        #endregion

        #region Public functions
        /// <summary>
        /// 加载配置信息
        /// </summary>
        public virtual void LoadConfiguration() { }

        /// <summary>
        /// 加载数字证书
        /// </summary>
        public virtual void LoadCredentials() { }

        /// <summary>
        /// 获取桌面控件
        /// </summary>
        /// <returns></returns>
        public virtual Grid GetDesk()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 加载模块
        /// </summary>
        /// <param name="module">模块</param>
        public virtual void LoadModule(Control module)
        {
            UnloadModule();

            GetDesk().Children.Add(module);
        }

        /// <summary>
        /// 卸载模块
        /// </summary>
        public virtual void UnloadModule()
        {
            GetDesk().Children.Clear();
        }
        #endregion

        #region Private functions
        /// <summary>
        /// 触发配置信息加载完成事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLoadConfigurationCompleted(EventArgs e)
        {
            if (LoadConfigurationCompleted != null)
            {
                LoadConfigurationCompleted(this, e);
            }
        }

        /// <summary>
        /// 触发数字证书加载完成事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLoadCredentialsCompleted(EventArgs e)
        {
            if (LoadCredentialsCompleted != null)
            {
                LoadCredentialsCompleted(this, e);
            }
        }

        /// <summary>
        /// 显示启动窗口
        /// </summary>
        private void ShowSplashWindow()
        {
            if (m_splashWindow == null)
            {
                m_splashWindow = new SplashWindow();
            }

            m_splashWindow.LoadSuccess += new EventHandler(SplashWindow_LoadSuccess);
            m_splashWindow.LoadFailure += new EventHandler(SplashWindow_LoadFailure);

            m_splashWindow.Show();
        }

        private void SplashWindow_LoadFailure(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void SplashWindow_LoadSuccess(object sender, EventArgs e)
        {
            this.Visibility = Visibility.Visible;

            var splashWindow = sender as SplashWindow;
            splashWindow.Close();
        }

        private void MainWindowBase_Initialized(object sender, EventArgs e)
        {
            if (IsEnableSplashWindow)
            {
                this.Visibility = Visibility.Hidden;

                ShowSplashWindow();
            }
        }
        #endregion

        #region Structure
        /// <summary>
        /// 创建主窗口对象
        /// </summary>
        public MainWindowBase()
        {
            Senvironments.MainWindow = this;

            this.Initialized += new EventHandler(MainWindowBase_Initialized);
        }
        #endregion

        #region Inner members
        /// <summary>
        /// 系统欢迎窗口
        /// </summary>
        protected SplashWindow m_splashWindow = null;
        #endregion
    }
}
